package j20251118;

public class MySingleLinkedList {
    static class Node{
        public int data;
        public Node next;
        public Node(int data){
            this.data = data;
        }
    }
    public Node head;
    //头插法
    public void addFirst(int data){
        if(head == null){
            head = new Node(data);
            return;
        }
        Node newNode=new Node(data);
        newNode.next=head;
        head=newNode;
    }
    //尾插法
    public void addLast(int data){
        if(head == null){
            head=new Node(data);
        }
        Node newNode=new Node(data);
        Node cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=newNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()-1){
            addLast(data);
            return;
        }
        Node cur=head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        Node newNode=new Node(data);
        newNode.next=cur.next;
        cur.next=newNode;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head==null){
            return false;
        }
        Node cur=head;
        while(cur!=null){
            if(cur.data==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head==null){
            return;
        }
        Node cur=head;
        Node prev=null;
        while(cur!=null){
            prev=cur;
            cur=cur.next;
            if(cur.data==key){
                break;
            }
        }
        prev.next = cur.next;
        cur.next = null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        Node cur=head;
        Node prev=null;
        while(cur!=null){
            prev=cur;
            cur=cur.next;
            if(cur!=null&&cur.data==key){
                prev.next = cur.next;
                cur.next = null;
                cur=prev.next;
            }
        }
        if(head.data==key){
            head=head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int len=0;
        Node cur=head;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }
    public void clear() {
        head=null;
    }
    public void display() {
        Node cur=head;
        while(cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
    }

    public static void main(String[] args) {
        MySingleLinkedList list=new MySingleLinkedList();
        list.addFirst(1);
        list.addLast(2);
        list.addLast(3);
        list.addIndex(3,1);
        list.display();
        System.out.println();
//        list.remove(1);
        list.removeAllKey(1);
        list.display();
        System.out.println();
        System.out.println(list.size());
        System.out.println(list.contains(1));
        System.out.println(list.contains(2));
        list.clear();
        list.display();
        System.out.println();
        System.out.println(list.size());

    }
}
